#!/bin/bash

# Setup $stat_flag based on OS type
mac_unix_name=Darwin
uname_returned=`uname -a`
unix_name=${uname_returned:0:${#mac_unix_name}}  	
if [ "$unix_name" = "$mac_unix_name" ]; then
  echo "MAC OS \"$mac_unix_name\" detected..." 	
  stat_flag=-f	  	
else	  	
  echo "non-MAC OS detected..."	  	
  stat_flag=-c	  	
fi

echo "Checking for syntax errors..."
for FILE in `git diff-index --name-only HEAD --` ; do
  if test -f $FILE; then
    # If a file is ruby, check for syntax errors using ruby
    if [[ "$FILE" =~ .*\.rb$ ]]; then
      if [[ "$fail" -eq 0 || -z "$fail" ]]; then
        ruby -c "$FILE" 1> /dev/null; fail=$?
        if [[ "$fail" -ne 0 ]]; then echo "Syntax Error found in '$FILE'"; fi
      else
        ruby -c "$FILE" 1> /dev/null
        if [[ "$?" -ne 0 ]]; then echo "Syntax Error found in '$FILE'"; fi
      fi
    fi

    # If a file is json, check for syntax errors
    if [[ "$FILE" =~ .*\.json$ ]]; then
      if [[ "$fail" -eq 0 || -z "$fail" ]]; then
        ruby -e "require 'rubygems'; require 'json'; JSON::parse(IO.read('$FILE'))" 1> /dev/null; fail=$?
        if [[ "$fail" -ne 0 ]]; then echo "Syntax Error found in '$FILE'"; fi
      else
        ruby -e "require 'rubygems'; require 'json'; JSON::parse(IO.read('$FILE'))" 1> /dev/null
        if [[ "$?" -ne 0 ]]; then echo "Syntax Error found in '$FILE'"; fi
      fi
    fi

    # If a file is yaml, check for syntax errors
    if [[ "$FILE" =~ .*\.yaml$ ]]; then
      if [[ "$fail" -eq 0 || -z "$fail" ]]; then
        ruby -e "require 'rubygems'; require 'yaml'; YAML::load(IO.read('$FILE'))" 1> /dev/null; fail=$?
        if [[ "$fail" -ne 0 ]]; then echo "Syntax Error found in '$FILE'"; fi
      else
        ruby -e "require 'rubygems'; require 'yaml'; YAML::load(IO.read('$FILE'))" 1> /dev/null
        if [[ "$?" -ne 0 ]]; then echo "Syntax Error found in '$FILE'"; fi
      fi
    fi
  fi
done
echo "Syntax check complete."

# Built-in git checks
git diff-index --check HEAD --

if [[ "$fail" -ne 0 && -n "$fail" ]]; then
  echo "Syntax Errors Found. Aborting commit"
  exit 1
fi

# Check for warnings
fail=0
for FILE in `git diff-index --name-only HEAD --` ; do
  if test -e $FILE; then
    # If a file is ruby, check for syntax errors
    if [[ -n `find $FILE -regex ".*\.rb$"` ]]; then
      warnings=`ruby -c "$FILE" 2>&1 | grep -i warn`
      if [[ -n "$warnings" ]]; then fail=1; fi
    fi
  fi
done

if [[ "$fail" -ne 0 && -n "$fail" ]]; then
  echo "Syntax Warnings Found. Aborting commit"
  exit 1
fi

# Check that username and user emails are filled properly
username=`git config --get user.name`
useremail=`git config --get user.email`
emaildomain=`echo $useremail | grep -o "[^@]*$"`
if [[ "$username" == "Put Your Name Here" || "$username" == "" ]]; then
  echo "Please set your git user.name by running 'git config user.name <your github username>'"
  exit 1
elif [[ "$useremail" == "setyouremail@rightscale.com" || "$useremail" == "" ]] || ! host "$emaildomain" &> /dev/null; then
  echo "Please set your git user.email by running 'git config user.email <your github email>'"
  exit 1
fi

exit 0
